package com.baizhi.serviceimpl;

import com.baizhi.dao.CategoryMapper;
import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Category;
import com.baizhi.entity.CategoryExample;
import com.baizhi.service.CategoryService;
import com.baizhi.vo.CommonQueryPageVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Resource
    CategoryMapper categoryMapper;

    @Override
    public CommonQueryPageVO queryOnePage(PageDTO pageDTO) {

        //创建条件对象
        CategoryExample example=new CategoryExample();
        example.createCriteria().andLevelsEqualTo(1);
        //根据条件查询一级类别的数量  条件是levels=1
        int count = categoryMapper.selectCountByExample(example);

        RowBounds rowBounds = new RowBounds((pageDTO.getPage()-1)*pageDTO.getPageSize(), pageDTO.getPageSize());
        //分页查询数据
        List<Category> categories = categoryMapper.selectByExampleAndRowBounds(example,rowBounds);

        CommonQueryPageVO commonQueryPageVO = new CommonQueryPageVO();
        commonQueryPageVO.setPage(pageDTO.getPage());
        commonQueryPageVO.setTotal(count);
        commonQueryPageVO.setRows(categories);

        return commonQueryPageVO;
    }
}
